13 while (cin
>> n
&& n
){
15 cin
>> start
, --start
;
19 while (cin
>> p
>> q
&& (p
+q
)){
23 for (int i
=0; i
<n
; ++i
){
24 sort(g
[i
].begin(), g
[i
].end());
27 int answer
= -1, length
= -1;
28 priority_queue
<pair
<int, int> > Q
; //First = distance, second = destiny
30 for (int i
=0; i
<n
; ++i
) visited
[i
] = -1;
31 Q
.push(make_pair(0, -start
));
33 int u
= -Q
.top().second
;
34 int w
= Q
.top().first
;
35 //cout << "Saque " << u + 1 << " " << w << endl;
38 if (visited
[u
] > w
) continue;
40 if (w
> length
|| (w
== length
&& u
< answer
)){ //En esta línea estaba el bug...
44 vector
<int> &vecinos
= g
[u
];
45 for (int i
=0; i
<vecinos
.size(); ++i
){
46 if (visited
[vecinos
[i
]] < w
+ 1){
47 visited
[vecinos
[i
]] = w
+ 1;
48 Q
.push(make_pair(w
+ 1, -vecinos
[i
]));
52 cout
<< "Case " << C
++ << ": The longest path from " << start
+ 1 << " has length ";
53 cout
<< length
<< ", finishing at " << answer
+ 1 << "." << endl
<< endl
;